<%@page import="cn.sendto.model.ResidentInfoModel"%>
<%@page import="cn.sendto.service.UserInfoService"%>
<%@page import="cn.sendto.service.ResidentInfoService"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8" errorPage="err.jsp"%>
<%
	//设置编码格式
	request.setCharacterEncoding("UTF-8");
	//接受表单数据
	String username = request.getParameter("username");
	String userpass = request.getParameter("userpass");
	String ranks = request.getParameter("ranks");
	String[] userreg = request.getParameterValues("userreg");

	//判断数据有效性
	if (username == null || username.equals("")) {
		request.setAttribute("message", "请输入用户名");
		request.getRequestDispatcher("login.jsp").forward(request, response);
		return;
	}
	if (userpass == null || userpass.equals("")) {
		request.setAttribute("message", "请输入密码");
		request.getRequestDispatcher("login.jsp").forward(request, response);
		return;
	}
	
	//判断用户类别
	session.setAttribute("ranks", ranks);
	if (ranks.equals("1")) {
		//学生
		ResidentInfoService ris = new ResidentInfoService();
		if (ris.dologin(username, userpass).size() > 0) {
			//是否记住用户
			if (userreg != null) {
				Cookie cook = new Cookie("user", username);
				cook.setMaxAge(60 * 60 * 24 * 7);
				cook.setPath("/");
				response.addCookie(cook);
			}
			session.removeAttribute("loginUser");
			//设置session
			session.setAttribute("ranks",ranks);
			//设置登陆状态
			session.setAttribute("loginResident", ris.dologin(username, userpass).get(0));
			request.getRequestDispatcher("indexLogin.jsp").forward(request, response);
			return;
		} else {
			request.setAttribute("message", "用户名或密码错误1");
			request.getRequestDispatcher("login.jsp").forward(request, response);
			return;
		}

	} else {
		//管理员
		UserInfoService uis = new UserInfoService();
			if (uis.dologin(username, userpass).size() > 0) {
				//记住用户
				if(userreg != null){
					Cookie cook = new Cookie("user", username);
					cook.setMaxAge(60 * 60 * 24 * 7);
					cook.setPath("/");
					response.addCookie(cook);
				}
				session.removeAttribute("loginResident");
				//设置session
				session.setAttribute("ranks",ranks);
				//记住登陆状态
				session.setAttribute("loginUser", uis.dologin(username, userpass).get(0));
				request.getRequestDispatcher("indexLogin.jsp").forward(request, response);
				return;
			}else {
			request.setAttribute("message", "用户名或密码错误");
			request.getRequestDispatcher("login.jsp").forward(request, response);
			return;
		}
	}
%>